#coding=utf8
from django.contrib import auth
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect

import settings

class AutoLogout:
    def process_request(self, request):
        if request.method == 'POST':
            username = request.POST.get('username',None)
            password = request.POST.get('password',None)
            if username and password:
                user = auth.authenticate(username=username, password=password)#获取登录状态
                if user is not None and user.is_active:
                    auth.login(request, user) #做登录处理
                    request.session['username'] = username
                    return HttpResponseRedirect('/%s/resource/show_resource/'%settings.SITE_ROOT)
                else:
                    message = "用户名口令错误！"
                    return render_to_response('permission/index.html',{'message':message})
            else:
                return
        if  not request.user.is_authenticated() :
            message = "请先登录！"
            auth.logout(request)
            return render_to_response('permission/index.html',{'message':message})
        else:
            return

class PermissionControl:
    def process_request(self, request):
        if request.path.find("/manage/")!=-1:
            user = request.user
            if user.has_perm('auth.change_group'):
                return
            else:
                return HttpResponseRedirect('/%s/no_permission/'%settings.SITE_ROOT);
        else:
            return
